package com.yh.blog.controller;

import com.yh.blog.domain.Message;
import com.yh.blog.domain.Users;
import com.yh.blog.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

/**
 * 留言前台展示控制层
 */
@Controller
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Value("${message.img}")
    private String messageImg;

//    跳转到留言页面
    @GetMapping("/message")
    public String message() {
        return "message";
    }

//    跳转到message页面中  th:fragment=messageList的标签
    @GetMapping("/messagecontent")
    public String message(Model model){
        model.addAttribute("messages",this.messageService.listMessage());
        return "message :: messageList";
    }

//    留言新增
    @PostMapping("/message")
    public String post(Message message, HttpSession session,Model model){

//        判断留言的人是否是站长 是|不是 进行不同的处理
        Users user=(Users) session.getAttribute("user");
        if(user!=null){
            message.setImg(user.getImg());
            message.setAdmin_message(true);
        }else {
            message.setImg(messageImg);
        }
        this.messageService.append(message);
        model.addAttribute("messages",this.messageService.listMessage());
        return "message :: messageList";
    }
}
